'''
@create_time: 2025/10/30 上午11:06
@Author: GeChao
@File: Out_warehouse_list_business.py
'''
from playwright.async_api import Page
from page.wms.out_warehouse.Out_warehouse_list_page import OutWarehouseListPage
from tools.log import Logger

logger = Logger().logger


def is_valid(value):
    """判断是否需要输入的有效值（排除None和空字符串）"""
    return value not in [None, ""]


class Out_warehouse_list_business(OutWarehouseListPage):
    """出库列表业务逻辑层"""

    def __init__(self, page: Page, **kwargs):
        super().__init__(page)
        self.out_warehouse_no = kwargs.get('出库单号')
        self.status = kwargs.get('状态')
        self.source_no = kwargs.get('来源单号')
        self.shipper = kwargs.get('货主')

    async def set_up(self):
        """进入出库列表页面"""
        # 检查"入库"菜单是否已展开
        # 检查"出库"菜单是否已展开
        is_visible = await (await self.click_input(role="menuitem")).text_filter("出库").is_visible()
        if not is_visible:
            # 如果入库菜单不可见，先点击 WMS 菜单展开
            await (await self.click_input(role="menuitem")).text_filter("WMS").click()
        await (await self.click_input(role="menuitem", name="出库")).nth(1).click()
        await (await self.click_input(role="link", name="出库单列表")).click()

    async def business(self):
        """业务处理"""
        await (await self.click_input(role="button", name="展开")).click()
        try:

            if is_valid(self.out_warehouse_no):
                await (await self.click_input(role="textbox", name="出库单号")).fill(self.out_warehouse_no)
                # await self.page.pause()
            if is_valid(self.status):
                await (await self.click_input(role="combobox", name="状态")).click()
                await (await self.text_element(text=self.status)).nth(0).click()
            if is_valid(self.source_no):
                await (await self.source_no_input).fill(self.source_no)
            # if is_valid(self.shipper):
            #     await (await self.click_input(role="combobox", name="货主")).click()
            #     await(await self.click_input(role="combobox", name="货主")).fill(self.shipper.lower())
            #     await (await self.search_dropdown(text=self.shipper)).click()

            # 点击搜索按钮 - 静态元素需要先 await 获取元素,再调用方法
            await (await self.search_button).click()
            await self.page.wait_for_timeout(3000)
            
            # 检查是否有搜索结果
            no_data_element = self.page.locator(".ant-empty-description")  # Ant Design 的空状态元素
            has_no_data = await no_data_element.count() > 0
            if has_no_data:
                # 搜索结果为空，根据预期判断是否失败
                logger.warning("⚠️ 搜索结果为空")
                return False  # 默认认为搜索结果为空就是失败
                
            if is_valid(self.out_warehouse_no):
                actual_no = await (await self.table_cell(row=0, column=2)).text_content(timeout=5000)
                assert actual_no == self.out_warehouse_no, f" 期望{self.out_warehouse_no}，实际{actual_no}"
            if is_valid(self.status):
                actual_status = await (await self.table_cell(row=0, column=3)).text_content(timeout=5000)
                assert actual_status in self.status, f"期望含有{self.status}, 实际{actual_status}"
            if is_valid(self.source_no):
                actual_source_no = await (await self.table_cell(row=0, column=4)).text_content(timeout=5000)
                assert actual_source_no == self.source_no, f"期望{self.source_no}, 实际{actual_source_no}"
            # if is_valid(self.shipper):
            #     actual_shipper = await (await self.table_cell(row=0, column=5)).text_content(timeout=5000)
            #     assert actual_shipper == self.shipper, f"期望{self.shipper}, 实际{actual_shipper}"
            return True
        except AssertionError as e:
            logger.error(e)
            return False
        except Exception as e:
            logger.error(e)
            return False